define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'customs/declaration/index' + location.search,
                    add_url: 'customs/declaration/add',
                    edit_url: 'customs/declaration/edit',
                    del_url: 'customs/declaration/del',
                    multi_url: 'customs/declaration/multi',
                    import_url: 'customs/declaration/import',
                    table: 'customs_declaration',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'orderNo', title: __('Orderno'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'logisticsNo', title: __('Logisticsno'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'acturalPaid', title: __('Acturalpaid'), operate:'BETWEEN'},
                        {field: 'goodsValue', title: __('Goodsvalue'), operate:'BETWEEN'},
                        {field: 'shipper', title: __('Shipper'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'shipperAddress', title: __('Shipperaddress'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'shipperTelephone', title: __('Shippertelephone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'shipperCountry', title: __('Shippercountry'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'buyerRegNo', title: __('Buyerregno'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'buyerName', title: __('Buyername'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'buyerIdNumber', title: __('Buyeridnumber'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'buyerTelephone', title: __('Buyertelephone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'consignee', title: __('Consignee'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'eProvince', title: __('Eprovince'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'consigneeCity', title: __('Consigneecity'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'consigneeZone', title: __('Consigneezone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'consigneeAddress', title: __('Consigneeaddress'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'consigneeTelephone', title: __('Consigneetelephone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'consigneeIdNumber', title: __('Consigneeidnumber'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'payTransactionId', title: __('Paytransactionid'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'declTime', title: __('Decltime'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'trafMode', title: __('Trafmode'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'insuredFee', title: __('Insuredfee'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'packageTypeCiq', title: __('Packagetypeciq'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'packageTypeCus', title: __('Packagetypecus'), searchList: {"1":__('Packagetypecus 1'),"2":__('Packagetypecus 2'),"3":__('Packagetypecus 3'),"4":__('Packagetypecus 4'),"5":__('Packagetypecus 5'),"6":__('Packagetypecus 6'),"7":__('Packagetypecus 7')}, formatter: Table.api.formatter.normal},
                        {field: 'grossWeight', title: __('Grossweight'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'netWeight', title: __('Netweight'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1'),"2":__('Status 2'),"3":__('Status 3')}, formatter: Table.api.formatter.status},
                        {
                            field: 'buttons',
                            width: "80px",
                            title: __('更多操作'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: __('立即申报'),
                                    title: __('立即申报'),
                                    classname: 'btn btn-success btn-ajax',
                                    icon: 'fa',
                                    url: 'customs/declaration/commit',
                                    confirm: '您确认要申报吗？',
                                    success: function (data, ret) {
                                        console.log(data)
                                        Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        table.bootstrapTable('refresh');
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    visible: function (row) {
                                        console.log(row.status)
                                        if(row.status == 1){
                                            return true;
                                        }
                                        //返回true时按钮显示,返回false隐藏
                                        return false;
                                    }
                                }
                            ],
                            formatter: Table.api.formatter.buttons,
                            operate: false
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
